﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Orgs;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.Orgs.Pages
{
    public partial class Employee_Edit : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String empyloyeeId = Request.QueryString["empyloyeeId"] ?? String.Empty;
            if (!IsPostBack)
            {
                BindEmployee(empyloyeeId);
            }
        }

        public void BindEmployee(string id)
        {
            if (id == "")
            {
                return;
            }
            OrgsService service = new OrgsService();
            Employee emloyee = service.GetEmployee(new Guid(id)).Data;

            this.txt_Code.Text = emloyee.Code;
            this.txt_Account.Text = emloyee.Account;
            this.txt_NameCN.Text = emloyee.NameCN;
            this.txt_NameEN.Text = emloyee.NameEN;
            this.txt_Email.Text = emloyee.Email;
            this.txt_Phone.Text = emloyee.Phone;
            this.txt_Sex.Text = emloyee.Sex.ToString();
            this.txt_IdCardNo.Text = emloyee.IdCardNo;
            this.txt_Birthday.Text = emloyee.Birthday.ToString();
        }

        protected void link_save_Click(object sender, EventArgs e)
        {
            String empyloyeeId = Request.QueryString["empyloyeeId"] ?? String.Empty;

            if (empyloyeeId == "")
            {
                return;
            }
            OrgsService service = new OrgsService();
            Employee emloyee = new Employee();

            emloyee.Code = this.txt_Code.Text;
            emloyee.Id = new Guid(empyloyeeId);
            emloyee.Account = this.txt_Account.Text;
            emloyee.NameCN = this.txt_NameCN.Text;
            emloyee.NameEN = this.txt_NameEN.Text;
            emloyee.Email = this.txt_Email.Text;
            emloyee.Phone = this.txt_Phone.Text;
            emloyee.Sex = Convert.ToInt32(this.txt_Sex.Text);
            emloyee.IdCardNo = this.txt_IdCardNo.Text;
            emloyee.Birthday = Convert.ToDateTime(this.txt_Birthday.Text);

            bool bl = service.SaveEmployee(emloyee).IsSuccess;
            if (bl)
            {
                Response.Redirect("Employee-List.aspx");
            }
        }

        protected void link_back_Click(object sender, EventArgs e)
        {
            Response.Redirect("Employee-List.aspx");
        }
    }
}